import argparse

TEMPORARY_PLAY_FILE = 'temporary-play.yml'

class AnsibleAutomation:

    def __init__(self, host_group, function_list):
        self.host_group = host_group
        self.function_list = function_list

    def generate_files(self):
        with open(TEMPORARY_PLAY_FILE, 'w') as play_file:
            play_file.write('---\n')
            play_file.write(f"- hosts: {self.host_group}\n")
            play_file.write(f"  remote_user: root \n")
            play_file.write(f"  gather_facts: no\n")
            play_file.write(f"  roles:\n")
            for function in self.function_list:
                play_file.write(f"  - role: {function}\n")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='自动化安装Linux模块')
    parser.add_argument('host_group', type=str, help='主机组或主机别名(在资产清单inventory.yml中配置)')
    parser.add_argument('function_list', type=str, help='''install-docker, install-harbor, install-jdk8, install-common-tools, install-mydumper ''')
    args = parser.parse_args()

    function_list = args.function_list.split(',')
    ansible_automation = AnsibleAutomation(args.host_group, function_list)
    ansible_automation.generate_files()

    import subprocess

    subprocess.run(['ansible-playbook', '-i', 'inventory.yml', '--vault-id', 'root@./ansible-valut-pw-file', TEMPORARY_PLAY_FILE])
